class Graph{
    constructor(isDirective=false){
        this.isDirective = isDirective;//图是有向图和无向图
        this.vertices = [];//存放顶点
        this.edges = new Map();
    }

    //添加顶点
    addVertex(v){
        //1.当前顶点是否已经添加
        if(!this.vertices.includes(v)){
            this.vertices.push(v);
            this.edges.set(v,[]);
        }
    }

    //添加边
    addEdge(v,w){
        if(!this.edges.get(v)){
            this.addVertex(v);
        }

        if(!this.edges.get(w)){
            this.addVertex(w);
        }

        this.edges.get(v).push(w);
        if(!this.isDirective){
            this.edges.get(w).push(v);
        }
    }

    //返回顶点列表
    getList(){
        return this.vertices;
    }

    getAdj(){
        return this.edges;
    }
    //toString
    toString(){
        let str = '';
        //1.循环顶点数组
        for(let i = 0;i<this.vertices.length;i++){
            console.log(this.vertices[i]);

            let list = this.edges.get(this.vertices[i]);

            console.log(`${this.vertices[i]}的相邻顶点个数:${list.length}`)

            str += `${this.vertices[i]}--->`;

            for(let j = 0;j<list.length;j++){
                str += list[j]
            }

            str += '\n';

            
        }
        return str
    }
}

let g = new Graph(true);

g.addVertex('A');

g.addVertex('B');

g.addEdge('A','C');

g.addEdge('B','C');

console.log(g.getAdj())
console.log(g.toString())